import WatchNumber from '@/utils/WatchNumber';

/** 帧数计算类 */
export default class FpsUtils {

	/** 显示的时间 */
	private showTime: number = 0;

	/** 保存的时间 */
	private saveTime: WatchNumber = new WatchNumber(0);

	/** 计算的帧数 */
	private fps: WatchNumber = new WatchNumber(0);

	/** 计算的帧数 */
	private refreshTime: WatchNumber = new WatchNumber(100);

	/** 保存的时间 */
	public get LastTime() {
		return this.saveTime;
	}

	/** 计算的帧数 */
	public get Fps() {
		return this.fps;
	}

	/** 刷新时间 */
	public get RefreshTime() {
		return this.refreshTime;
	}

	/**
	 * 触发更新
	 */
	public Trigger(): void {
		const showTime = this.showTime;
		const saveTimeV = this.saveTime.Value;
		const refreshTimeV = this.refreshTime.Value;
		const now: number = (+new Date());
		this.saveTime.Value = now;
		if (now - showTime >= refreshTimeV) {
			this.showTime = now;
			this.fps.Value = 1000 / (now - saveTimeV);
		}
	}
}
